package top.mall.happy.oauth.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import top.mall.happy.domain.ClUser;
import top.mall.happy.oauth.domain.UserInfo;
import top.mall.happy.oauth.utils.MD5Util;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        // 根据名称去查数据库
        ClUser clUser = new ClUser();
        clUser.setUserName(username);
        clUser.setPassword("123456");

        // 权限列表
        List<String> user_permission = new ArrayList<>();
        String user_permission_string = StringUtils.join(user_permission.toArray(), ",");
        user_permission.add("admin");
        return new UserInfo(username, MD5Util.getStringMD5(clUser.getPassword()), AuthorityUtils.commaSeparatedStringToAuthorityList(user_permission_string));
    }
}
